
te<template>
  <el-dialog
    title="导入模板"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose">
    <div style="margin-bottom:10px">
      <el-button @click="downloadFile()">下载模板</el-button>
    </div>
    <el-upload
      action="#"
      :auto-upload="false"
      :on-change="handleFileChange"
      :show-file-list="true"
      accept=".xlsx"
    >
      <el-button size="small" type="primary">点击上传文件</el-button>
    </el-upload>

    <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleSubmit()" :loading="loading">提交</el-button>
        </span>
  </el-dialog>
</template>
<script>

export default {
  name: 'TeachingGrades',
  props: {
    importApi: {
      type: Function,
      default: () => Promise.resolve()
    },
    templateUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      file: '',
      disabled: false,
      loading: false,
      id: '',
      dialogVisible: false,
      selectedFile: '',
    }
  },
  created() {

  },
  methods: {
    handleFileChange(file, fileList) {
      this.selectedFile = file.raw;
    },
    handleSubmit() {
      const form = new FormData();
      form.set('file', this.selectedFile);
      this.loading = true;
      this.importApi(form).then(res => {
        if(res.code === 200){
          this.loading = false;
          this.$message.success(res.msg || '导入成功!');
          this.$emit('uploadSuccess')
          this.dialogVisible = false;
        }
      })
    },
    handleClose() {
      this.dialogVisible = false;
    },
    open(id) {
      this.dialogVisible = true;
      this.id = id;
    },
    downloadFile() {
      window.open(this.templateUrl, "_blank");
    }
  }
}
</script>
